<?php namespace App\Core\Repository\Criterias;

use App\Core\Repository\Criteria;
use App\Core\Repository\Repository;
use App\Core\Exceptions\RuntimeError;

class GroupByCriteria extends Criteria {
    
    protected $field;

    public function __construct($field) {
        if(!$field){
            throw new RuntimeError('SortCriteria error.field name error.');
        }
        $this->field = $field;
        $this->setName('group_by_'.$this->field);
    }

    public function apply(Repository $repository){
        parent::apply($repository);
        $this->query->groupBy($this->formatField($this->model, $this->field));
    }
}